/**
 * GeneralAPIFunctionAlgorithm.java
 * ************************** 
 * @date May 28, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.algorithms;

import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import gossipServices.basic.nodeDescriptors.NodeDescriptor;
import infrastructureAPI.Requester;
import infrastructureAPI.Service;
import infrastructureAPI.APIMessages.DispatcherRequestMessage;
import infrastructureAPI.APIMessages.RequestMessage;

import java.net.InetAddress;
import java.util.List;

/**
 * Description: 
 * The class GeneralAPIFunctionAlgorithm 
 * can be used as a base class for functions that
 * provide algorithm of the infrastructure level
 * and have to implements the APIFunction.
 * The mandatory constructor need the Requester node
 * as a unique parameter.
 * We suggest to avoid the use of the empty constructor,
 * specially for the submission of a request.
 *
 */
public abstract class GeneralAPIFunctionAlgorithm
		implements APIFunction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Requester requester;
	
	protected List<Service> services;
	protected boolean returnValue = true;
	
	protected GeneralAPIFunctionAlgorithm(){}

	/**
	 * 
	 */
	public GeneralAPIFunctionAlgorithm(Requester requester) {
		super();
		this.requester = requester;
	}
	
	protected RequestMessage defaultRequestMessageComposition(InetAddress localAddress,
			String localNodeName){
		NodeDescriptor sender =
			new ConcreteNodeDescriptor(localNodeName, localAddress , null);
		return new DispatcherRequestMessage(sender, sender, this, requester);
	}
	
	public boolean initializeServices(List<Service> services){
		this.services = services;
		/*
		 *  This is the complete check.
		 *  Perhaps in future we will require it. 
		 */
//		returnValue = true;
//		for(int i=0; i<services.size(); i++){
//			returnValue = services.get(i).equals(requiredServices().get(i));
//		}
		returnValue = services.size() == requiredServices().size();
		return returnValue;
	}
	
}